﻿Imports System.Reflection

Public Class InitForm

    Dim _配置表 As DataTable

    Private Sub btn开始_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn开始.Click

        _配置表 = DBUtil.组件配置表

        btn开始.Enabled = False
        dgStatus.Rows.Clear()

        IListenDb.CommonConUtil.连接字符串 = MyConfig.连接字符串

        更新状态("开始创建成功")

        Dim iTable As IListenDb.ITable
        Dim iTrigger As Wys.IListenDb.ITrigger
        Dim assembleName As String

        If Me.rad设置监听.Checked = False Then

            For Each lst As 组件表 In 组件表操作类.GetAll组件
                assembleName = lst.组件名称
                '开始删除触发器
                iTrigger = CType(Assembly.Load(assembleName).CreateInstance(assembleName & ".CreateTrigger"), Wys.IListenDb.ITrigger)
                If iTrigger IsNot Nothing AndAlso lst.触发器 = 1 AndAlso lst.删除触发器 = 1 Then

                    AddHandler iTrigger.CreateTableTrigger, AddressOf 更新状态
                    AddHandler iTrigger.UpdateTableTrigger, AddressOf 更新状态
                    AddHandler iTrigger.DeleteTableTrigger, AddressOf 更新状态

                    iTrigger.DeleteTrigger(IListenDb.CommonConUtil.GetCon, MyConfig.前缀)
                End If
            Next

            For Each lst As 组件表 In 组件表操作类.GetAll组件
                '开始删除表
                assembleName = lst.组件名称
                iTable = CType(Assembly.Load(assembleName).CreateInstance(assembleName & ".CreateTable"), IListenDb.ITable)
                If iTable IsNot Nothing AndAlso lst.表 = 1 AndAlso lst.删除表 = 1 Then

                    AddHandler iTable.CreateTableEvent, AddressOf 更新状态
                    AddHandler iTable.UpdateTableEvent, AddressOf 更新状态
                    AddHandler iTable.DeleteTableEvent, AddressOf 更新状态

                    iTable.DeleteTable(IListenDb.CommonConUtil.GetCon, MyConfig.前缀)
                End If
            Next

        End If

        '开始创建
        If rad取消监听.Checked = False Then

            '开始创建表
            For Each lst As 组件表 In 组件表操作类.GetAll组件
                assembleName = lst.组件名称
                iTable = Assembly.Load(assembleName).CreateInstance(assembleName & ".CreateTable")
                If iTable IsNot Nothing AndAlso lst.表 = 1 AndAlso lst.添加表 = 1 Then

                    AddHandler iTable.CreateTableEvent, AddressOf 更新状态
                    AddHandler iTable.UpdateTableEvent, AddressOf 更新状态
                    AddHandler iTable.DeleteTableEvent, AddressOf 更新状态

                    iTable.CreateTable(IListenDb.CommonConUtil.GetCon, MyConfig.前缀)
                End If
            Next

            '开始更新表
            For Each lst As 组件表 In 组件表操作类.GetAll组件
                assembleName = lst.组件名称
                iTable = Assembly.Load(assembleName).CreateInstance(assembleName & ".CreateTable")
                If iTable IsNot Nothing AndAlso lst.表 = 1 AndAlso lst.修改表 = 1 Then

                    AddHandler iTable.CreateTableEvent, AddressOf 更新状态
                    AddHandler iTable.UpdateTableEvent, AddressOf 更新状态
                    AddHandler iTable.DeleteTableEvent, AddressOf 更新状态

                    iTable.UpdateTable(IListenDb.CommonConUtil.GetCon, MyConfig.前缀)
                End If
            Next

            '开始创建触发器
            For Each lst As 组件表 In 组件表操作类.GetAll组件
                assembleName = lst.组件名称
                iTable = Assembly.Load(assembleName).CreateInstance(assembleName & ".CreateTable")
                iTrigger = Assembly.Load(assembleName).CreateInstance(assembleName & ".CreateTrigger")
                If iTrigger IsNot Nothing AndAlso lst.触发器 = 1 AndAlso lst.添加触发器 = 1 Then

                    AddHandler iTrigger.CreateTableTrigger, AddressOf 更新状态
                    AddHandler iTrigger.UpdateTableTrigger, AddressOf 更新状态
                    AddHandler iTrigger.DeleteTableTrigger, AddressOf 更新状态

                    iTrigger.CreateTrigger(IListenDb.CommonConUtil.GetCon, MyConfig.前缀)
                End If
            Next

            '开始更新触发器
            For Each lst As 组件表 In 组件表操作类.GetAll组件
                assembleName = lst.组件名称
                iTrigger = Assembly.Load(assembleName).CreateInstance(assembleName & ".CreateTrigger")
                If iTrigger IsNot Nothing AndAlso lst.触发器 = 1 AndAlso lst.修改触发器 = 1 Then

                    AddHandler iTrigger.CreateTableTrigger, AddressOf 更新状态
                    AddHandler iTrigger.UpdateTableTrigger, AddressOf 更新状态
                    AddHandler iTrigger.DeleteTableTrigger, AddressOf 更新状态

                    iTrigger.UpdateTrigger(IListenDb.CommonConUtil.GetCon, MyConfig.前缀)
                End If
            Next

        End If

        IListenDb.CommonConUtil.CloseCon()
        更新状态("创建成功")

        btn开始.Enabled = True

    End Sub

    Private Sub 更新状态(ByVal content As String)

        dgStatus.Rows.Insert(0, New Object() {content})
        Application.DoEvents()

    End Sub

    Private Sub btn取消_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn取消.Click

        Me.Close()

    End Sub

    Private Sub InitForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim ct As New wys.util.Util.ConUtil(MyConfig.连接字符串)

        txtCon.Text = ct.DataSource

        txtDb.Text = ct.DataBase

        txtUser.Text = ct.User

        txtPwd.Text = ct.Pwd

    End Sub

End Class
